package cn.ekuma.security.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ekuma.security.bean.I_Permission;
import cn.ekuma.security.bean.I_RolePermission;

public class AbstractRolePermission implements I_RolePermission {
	
	static final AdminPermission ADMIN=new AdminPermission();
	
	Map<Integer,I_Permission> permissions;
	
	Map<String,I_Permission> permissions1;
	
	public void setPermissions(List<I_Permission> ps){
		if(permissions==null)
			permissions=new HashMap<Integer,I_Permission>();
	    if(permissions1==null)
	    	permissions1=new HashMap<String,I_Permission>();
		for(I_Permission p:ps){
			permissions.put(p.getId(), p);	
			permissions1.put(p.getBaseSecurityName(), p);
		}
	}
	

	@Override
	public boolean hasPermission(int grupPermission) {
		if(permissions.containsKey(ADMIN.getId()))
			return true;
		if(permissions.containsKey(grupPermission))
			return true;
		return false;
	}


	@Override
	public int getExtPermission(int grupPermission) {
		if(permissions.containsKey(grupPermission))
			return permissions.get(grupPermission).getExtPermission();
		return I_Permission.NO_PASS;
	}


	@Override
	public int getPermissionIdByName(String name) {
		return permissions1.get(name).getId();
	}


	@Override
	public boolean hasPermissionByName(String checkPermission) {
		return permissions1.containsKey(checkPermission);
	}
}
